package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.goods.Category;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.goods.CategoryService;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.service.order.CartService;
import com.qingcheng.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private SkuService skuService;

    @Reference
    private CategoryService categoryService;

    @Override
    public List<Map<String, Object>> findCartList(String username) {
        System.out.println("从redis中读取购物车");
        List<Map<String, Object>> cartList = (List<Map<String, Object>>)redisTemplate.boundHashOps(CacheKey.CART_LIST).get(username);
        if (cartList==null){
            return new ArrayList<>();
        }
        return cartList;
    }

    @Override
    public void addItem(String username, String skuId, Integer num) {
        //实现思路： 遍历购物车，如果购物车中存在该商品则累加数量，如果不存在则 添加购物车项

        //1获取购物车
        List<Map<String, Object>> cartList = findCartList(username);
        //2遍历购物车
        boolean flag = false;
        for (Map map : cartList) {
            //2.1根据skuId查询商品是否存在购物车
            OrderItem orderItem = (OrderItem) map.get("item");
            if (orderItem.getSkuId().equals(skuId)){
                if (orderItem.getNum()<num){
                    cartList.remove(map);
                    break;
                }
                int weight= orderItem.getWeight() / orderItem.getNum();
                orderItem.setNum(orderItem.getNum()+num);
                orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
                orderItem.setWeight(weight*orderItem.getNum());
                if (orderItem.getNum()<num){
                    cartList.remove(map);
                }
                flag = true;
                break;
            }
        }
        //购物车里无商品
        if (flag = false){
            Sku sku = skuService.findById(skuId);
            if (sku==null){
                throw new RuntimeException("商品不存在");
            }
            if (!"1".equals(sku.getStatus())){
                throw new RuntimeException("商品状态不合法");
            }
            if (num <= 0){
                throw new RuntimeException("商品数量不合法");
            }

            OrderItem orderItem = new OrderItem();

            orderItem.setSkuId(skuId);
            orderItem.setSpuId(sku.getSpuId());
            orderItem.setNum(num);
            orderItem.setImage(sku.getImage());
            orderItem.setPrice(sku.getPrice());
            orderItem.setName(sku.getName());
            orderItem.setMoney(sku.getPrice()*num);
            if (sku.getWeight()==null){
                orderItem.setWeight(0);
            }
            orderItem.setWeight(sku.getWeight()*num);

            //商品分类
            orderItem.setCategoryId3(sku.getCategoryId());//三级分类
            Category category3 =(Category) redisTemplate.boundHashOps(CacheKey.CATEGORY).get(sku.getCategoryId());
            if (category3==null){
                category3 = categoryService.findById(sku.getCategoryId());
                redisTemplate.boundHashOps(CacheKey.CATEGORY).put(sku.getCategoryId(),category3);
            }
            orderItem.setCategoryId2(category3.getParentId());//二级分类
            Category category2 = (Category) redisTemplate.boundHashOps(CacheKey.CATEGORY).get(category3.getParentId());
            if (category2==null){
                category2 = categoryService.findById(category3.getParentId());
                redisTemplate.boundHashOps(CacheKey.CATEGORY).put(category3.getParentId(),category2);
            }
            orderItem.setCategoryId1(category2.getParentId());//一级分类

            Map map = new HashMap();
            map.put("item",orderItem);
            map.put("checked",true);
            cartList.add(map);
        }

        redisTemplate.boundHashOps(CacheKey.CART_LIST).put(username,cartList);
    }
}
